'use strict';

import { platform_agentcase_recordModel } from "../model/platform_agentcase_record";
import { ServiceBase, Exceptions } from '../kalend';
import { platform_userService } from "../service/platform_user";
import { common } from "../middleware/common";

class agentcashrecords extends ServiceBase {
    constructor() {
        super(platform_agentcase_recordModel)
    }

    //分页查询提现记录
    async querybypage(params: any) {
        const offset = params.offset;
        const limit = params.limit;
        const starttime = params.sdate;
        const endtime = params.edate;
        const auditstatus = params.auditstatus;
        const uid = params.userid;
        const filter: any = {};

        if (uid) {
            // let merchants: any = await merchantService.queryByUserId({
            //     userid: uid
            // });
            // let ids = underscore.pluck(merchants.rows, '_id');
            filter.agentid = uid;
        }


        if (starttime) {
            filter.updateTime = { '$gte': new Date(starttime) };
        }
        if (endtime) {
            if (filter.updateTime) {
                filter.updateTime['$lte'] = new Date(endtime);
            } else {
                filter.updateTime = { '$lte': new Date(endtime) };
            }
        }

        if (auditstatus) {
            filter.auditstatus = auditstatus;
        }

        let result = await super.queryPagination(offset, limit, {
            filter: filter
        });

        result.rows = result.rows.map((item) => {


            if (item.auditstatus == 0) {
                item.auditstatus = "审核中";
            } else if (item.auditstatus == 1) {
                item.auditstatus = "审核通过";
            } else if (item.auditstatus == 2) {
                item.auditstatus = "审核未通过";
            }
            if (item.casestatus == 0) {
                item.casestatus = "未打款";
            } else {
                item.casestatus = "已打款";
            }
            return item;
        });
        return result;
    }

    //创建提现记录
    async craeterecord(doc: any) {
        //验证是否有存在未审核得提现
        let nocases: any = await this.queryAll({
            filter: {
                casestatus: 0, agentid: doc.agentid
            }
        })
        if (nocases.length > 0) {
            return "存在未打款记录，打款完毕后才可以申请下一次提现"
        }
        if (doc.agentid) {
            await this.create(doc);
            let puser = await platform_userService.queryById(doc.agentid);
            if (doc.type === '奖励账户') {
                let sy = common.sub(puser.balance, Number(doc.totalcase));
                await platform_userService.update(doc.agentid, { balance: sy });
            } else if (doc.type === '招募账户') {
                let sy = common.sub(puser.balancezm, Number(doc.totalcase));
                await platform_userService.update(doc.agentid, { balancezm: sy });
            }
            return "提现申请成功，请等待审核打款";
        }
    }


    //提现审核
    async audit(pars: any) {
        const asta = pars.auditstatus;
        const csta = pars.casestatus;

        let doc: any = await this.querySingle({ _id: pars._id });
        if (asta) {
            if (doc.auditstatus == 0) {
                //通过
                if (pars.AuditStatus == 1) {

                } else if (pars.AuditStatus == 2) {
                    // 拒绝
                    let puser = await platform_userService.queryById(doc.agentid);
                    if (doc.type === '奖励账户') {
                        let sy = common.add(puser.balance, Number(doc.totalcase));
                        await platform_userService.update(doc.agentid, { balance: sy });
                    } else if (doc.type === '招募账户') {
                        let sy = common.add(puser.balancezm, Number(doc.totalcase));
                        await platform_userService.update(doc.agentid, { balancezm: sy });
                    }
                }
                return this.update(pars._id, { auditstatus: pars.auditstatus });
            }
        }

        if (csta) {
            if (doc.auditstatus == 1 && doc.casestatus == 0) {
                //let user: any = await platform_userService.querySingle({ _id: one.agentid })
                //扣减会员余额
                //let sy = common.sub(user.balance, one.totalcase);
                //let sy=user.balance-one.totalcase;
                //platform_userService.update(one.agentid, { balance: sy })
                return this.update(pars._id, { casestatus: csta });
            }
        }
    }

    //查询品牌大使未审核提现订单
    async queryagentnoaudit(doc: any) {
        return await this.queryAll({
            filter: {
                casestatus: 0
            }
        })
    }
}

export const agentcashrecordsService = new agentcashrecords();